package cn.udrm.water.base.v1.baseconfig.restful;

import java.util.List;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.udrm.water.base.v1.baseconfig.dto.IndexMapConfigDto;
import cn.udrm.water.base.v1.baseconfig.service.IndexMapConfigService;
import cn.udrm.water.base.v1.commons.models.ResponseParam;

@Api(tags="首页地图配置实体API", description="首页地图操作")
@RestController
@RequestMapping(value="v1/restful/indexMapConfig")
public class IndexMapConfigRestful {

	@Autowired
	private IndexMapConfigService indexMapConfigService;
	
	@ApiOperation("获取启用的地图配置实体")
	@GetMapping(value="useConfigIsTrue")
	public ResponseParam<IndexMapConfigDto> useConfigIsTrue() {
		IndexMapConfigDto useConfigIsTrue = indexMapConfigService.useConfigIsTrue();
		return new ResponseParam<IndexMapConfigDto>("数据获取成功", 200, useConfigIsTrue);
	}
	
	@ApiOperation("数据列表获取")
	@GetMapping(value="list")
	public ResponseParam<List<IndexMapConfigDto>> list() {
		List<IndexMapConfigDto> list = indexMapConfigService.findAll();
		return new ResponseParam<List<IndexMapConfigDto>>("数据获取成功", 200, list);
	}
	
	@ApiOperation("单条数据获取")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="渠道GUID")
	@GetMapping(value="get/{id}")
	public ResponseParam<IndexMapConfigDto> get(@PathVariable(value="id") String id) {
		IndexMapConfigDto IndexMapConfigDto = indexMapConfigService.getAllById(id);
		return new ResponseParam<IndexMapConfigDto>("数据获取成功", 200, IndexMapConfigDto);
	}
	
	@ApiOperation("数据保存")
	@ApiImplicitParam(name="IndexMapConfigDto", value="需要添加的数据", required=true, dataType="IndexMapConfigDto", dataTypeClass=IndexMapConfigDto.class)
	@PostMapping(value="save")
	public ResponseParam<IndexMapConfigDto> save(@RequestBody IndexMapConfigDto IndexMapConfigDto) {
		IndexMapConfigDto = indexMapConfigService.save(IndexMapConfigDto);
		
		return new ResponseParam<IndexMapConfigDto>("数据保存成功", 200, IndexMapConfigDto);
	}
	
	@ApiOperation("数据更新")
	@ApiImplicitParam(name="IndexMapConfigDto", value="需要更新的数据", required=true, dataType="IndexMapConfigDto", dataTypeClass=IndexMapConfigDto.class)
	@PostMapping(value="update")
	public ResponseParam<IndexMapConfigDto> update(@RequestBody IndexMapConfigDto IndexMapConfigDto) {
		if (IndexMapConfigDto == null || StringUtils.isBlank(IndexMapConfigDto.getId())) {
			return new ResponseParam<IndexMapConfigDto>("待更新数据没有主键", 500);
		}
		IndexMapConfigDto = indexMapConfigService.update(IndexMapConfigDto);
		
		return new ResponseParam<IndexMapConfigDto>("数据更新成功", 200, IndexMapConfigDto);
	}

	@ApiOperation("数据删除（仅删除自身，不能删除标识有实体间关系）")
	@ApiImplicitParam(paramType="path", name="id", dataType="String", required=true, value="数据GUID")
	@GetMapping(value="deleteForce/{id}")
	public ResponseParam<String> deleteForce(@PathVariable(value="id") String id) {
		try {
			indexMapConfigService.deleteAllSelf(new String[]{id});
		} catch (Exception e) {
			return new ResponseParam<String>("删除失败", 500);
		}
		return new ResponseParam<String>("删除成功", 200);
	}
}
